﻿using Comm.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Comm.Web.Action
{
    /// <summary>
    /// car 的摘要说明
    /// </summary>
    public class car : BaseHander
    {
        BLL.CartService bllcar = new BLL.CartService();
        BLL.CarItemsService bllItems = new BLL.CarItemsService();
        Model.Cart cart = null;
        public override void SonLoad()
        {
            string type = context.Request.Params["type"];
            switch (type)
            {
                case "ac":
                    AddCar();
                    break;
                case "d":
                    DoDel();
                    break;
                case "mnum":
                     DoUpdate();
                    break;
            }
        }

        #region 1.0给用户加一个购物车+ void AddCar()
        public void AddCar()
        {
            string pid = Request.Form["pid"];
            if (!ValidateHelper.IsNum(pid))
            {
                //如果验证失败
                AjaxMsgHelper.AjaxMsg("err", "参数异常~！");
                Response.End();
            }
            else
            {
                cart = bllcar.GetUserCar(UserNow.uId, 0);
                bllcar.AddBookItem(int.Parse(pid), cart);
                //加入购物车完毕后，返回消息
                AjaxMsgHelper.AjaxMsg("ok", "加入成功~~~");
            }
        } 
        #endregion

        public void DoDel()
        {
            string bookid = Request.Form["mid"];
            if (!ValidateHelper.IsNum(bookid))
            {
                //如果验证失败
                AjaxMsgHelper.AjaxMsg("err", "参数异常~！");
                Response.End();
            }
            else
            {
                bllItems.Delete1(int.Parse(bookid),UserNow.uId);
                AjaxMsgHelper.AjaxMsg("ok", "删除成功~~~");
            }
        }

        public void DoUpdate()
        {
            System.Threading.Thread.Sleep(1000);

            //1.接收 产品 id 和 购物车里该产品数量
            string _bookId = Request.Form["bid"];
            string strPNum = Request.Form["num"];
            string itemsId = Request.Form["itemsId"];
            Model.CarItems _model = new Model.CarItems() 
            { 
               itemId=int.Parse(itemsId),
               bookId=int.Parse(_bookId),
               count= int.Parse(strPNum)
            };
            bllItems.Update1(_model);
            AjaxMsgHelper.AjaxMsg("ok", "更新数量成功~~");
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}